package priv.aopt;

import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.lang.reflect.Method;

import org.aspectj.lang.ProceedingJoinPoint;
import org.aspectj.lang.Signature;
import org.aspectj.lang.annotation.Around;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.aspectj.lang.reflect.MethodSignature;
import org.springframework.stereotype.Component;

@Component
@Aspect
public class JinChaAop {
	@Pointcut("execution(* priv.aopt.Person.*(String))")
	public void zq() {

	}

	@Pointcut("@annotation(priv.aopt.DaGu)")
	public void dg() {

	}

	@Pointcut("@annotation(priv.aopt.FromFile)")
	public void FromFile() {

	}

	@Around("@annotation(ff)")
	public Object round(ProceedingJoinPoint pjp, FromFile ff) {
		// System.out.println(ff.value());

		/*
		 * MethodSignature signature = (MethodSignature) pjp.getSignature(); Method
		 * method = signature.getMethod(); priv.aopt.FromFile annotation =
		 * method.getAnnotation(FromFile.class); String value = annotation.value();
		 * System.out.println(value);
		 */

		System.out.println(ff.value());

		String value = ff.value();

		Object[] args = pjp.getArgs();
		try (FileReader fileReader = new FileReader(value);
				BufferedReader bufferedReader = new BufferedReader(fileReader)) {
			String str = "";
			StringBuffer sb = new StringBuffer();
			while ((str = bufferedReader.readLine()) != null) {
				sb.append(str);

			}
			
			/*for (int i = 0; i < args.length; i++) {
				if (args[i] instanceof String) {
					args[i]=sb.toString();break;
				}
			}*/
			return pjp.proceed(new Object[] { sb.toString() });
		} catch (Throwable e) {

			e.printStackTrace();
		} 
		return null;
	}
}
